﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using TheBullysStore.Models.ModuloAdministracion.ReservaAreaComun;
namespace TheBullysStore.Controllers.ModuloAdministracion.ReservaAreaComun
{
    public class ReservaAreaComunController : Controller
    {
        //
        // GET: /Reunion/

        ReservaAreaComunDAO reservaDao = new ReservaAreaComunDAO();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ReservaAreaComun()
        {
            return View();
        }


        public ActionResult BuscarReservaAreaComun()
        {
            return View();
        }


        public ActionResult RegistrarReservaAreaComun()
        {
            return View();
        }



        [HttpPost]
        public JsonResult insertarReserva(ReservaAreaComunBean reservaBean)




        {
            try
            {
                List<string> dia;
                List<List<string>> lista = new List<List<string>>();
                List<string> listaDisponibilidad = reservaDao.verDisponibilidad(reservaBean.idAreaComun);
                List<DiaReserva> listaDiaReserva = reservaDao.verListaReserva(reservaBean.idAreaComun, getFechas(reservaBean.fecha));

                int min, max;
                max = min = 0;
                int a = 1;
                string[] aux;
                foreach (string disp in listaDisponibilidad)
                {
                    aux = disp.Split('-');
                    if (a == 1)
                    {

                        min = Convert.ToInt32(aux[0]);
                        max = Convert.ToInt32(aux[1]);
                        a = 2;
                    }
                    else
                    {
                        if (Convert.ToInt32(aux[0]) < min) { min = Convert.ToInt32(aux[0]); }
                        if (Convert.ToInt32(aux[1]) > max) { max = Convert.ToInt32(aux[1]); }

                    }
                }
                int i = 0;
                int x, y;
                string[,] matriz = new string[7, max - min];
                for (int n = 0; n < 7; n++)
                    for (int m = 0; m < (max - min); m++)
                    {
                        matriz[n, m] = "-1";

                    }

                int menor, mayor;
                i = 0;
                foreach (string disp in listaDisponibilidad)
                {
                    aux = disp.Split('-');

                    menor = Convert.ToInt32(aux[0]);
                    mayor = Convert.ToInt32(aux[1]);
                    int aa = (menor - min);
                    for (int bb = 0; bb < (mayor - menor); bb++)
                    {
                        matriz[i, aa] = "0";
                        aa++;
                    }
                    i++;
                }




                i = 0;
                foreach (DiaReserva diaReserva in listaDiaReserva)
                {

                    foreach (Reserva reserva in diaReserva.listaReserva)
                    {
                        x = Convert.ToInt32(reserva.hI);
                        y = Convert.ToInt32(reserva.hF);
                        while (x < (y))
                        {
                            matriz[i, (x - min)] = "1";
                            x++;
                        }



                    }
                    i++;
                }

                switch (reservaBean.fecha.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        i = 1; break;
                    case DayOfWeek.Tuesday:
                        i = 2; break;
                    case DayOfWeek.Wednesday:
                        i = 3; break;
                    case DayOfWeek.Thursday:
                        i = 4; break;
                    case DayOfWeek.Friday:
                        i = 5; break;
                    case DayOfWeek.Saturday:
                        i = 6; break;
                    case DayOfWeek.Sunday:
                        i = 7; break;
                }
                i--;






                string mensaje;
                try
                {

                    x = Convert.ToInt32(reservaBean.horaInicio);
                    y = Convert.ToInt32(reservaBean.horaFin);
                    while (x < (y))
                    {
                        if (matriz[i, (x - min)] != "0")
                        {
                            mensaje = "El rango de tiempo seleccionado no es valido, seleccione otro";
                            return Json(new { mensaje });
                        }

                        matriz[i, (x - min)] = "1";
                        x++;
                    }


                }
                catch (Exception ex)
                {


                    mensaje = "El rango de tiempo seleccionado no es valido, seleccione otro";
                    return Json(new { mensaje });

                }



















                mensaje = reservaDao.insertarReserva(reservaBean);

                return Json(new { mensaje });

            }
            catch (Exception exx)
            {
                string mensaje = "Ocurrio un error Desconocido.";

                return Json(new { mensaje });

            }
        }

       








        [HttpPost]
        public JsonResult verDisponibilidad(int idAreaComun,DateTime fecha)
        {

            List<string> dia;
             List<List<string>> lista = new List<List<string>>();
            List<string> listaDisponibilidad = reservaDao.verDisponibilidad(idAreaComun);
            List<DiaReserva> listaDiaReserva = reservaDao.verListaReserva(idAreaComun,getFechas( fecha));

            int min, max;
            max=min = 0;
            int a = 1;
            string[] aux;
           foreach ( string disp in listaDisponibilidad ) {
               aux = disp.Split('-');
               if (a == 1)
               {

                   min = Convert.ToInt32(aux[0]);
                   max= Convert.ToInt32(aux[1]);
                   a = 2;
               }
               else
               {
                    if(Convert.ToInt32(aux[0]) < min){min = Convert.ToInt32(aux[0]);}
                    if (Convert.ToInt32(aux[1]) > max) { max = Convert.ToInt32(aux[1]); }
                   
               }
           }
            int i=0;
            int x,y;
           string[,] matriz=new string[7,max-min];
            for(int n=0;n<7;n++)
                for(int m=0;m<(max-min);m++){
    matriz[n,m]="-1";

                }

            int menor, mayor;
            i = 0;
            foreach (string disp in listaDisponibilidad)
            {
                aux = disp.Split('-');

                menor = Convert.ToInt32(aux[0]);
                mayor = Convert.ToInt32(aux[1]);
      int aa=(menor-min);              
 for (int bb=0;bb<(mayor-menor) ;bb++)
                {
                    matriz[i, aa] = "0";
                    aa++;
                }
 i++;
            }




            i = 0;
            foreach (DiaReserva diaReserva in listaDiaReserva)
            {

                foreach (Reserva reserva in diaReserva.listaReserva)
                {
                    x = Convert.ToInt32(reserva.hI);
                    y = Convert.ToInt32(reserva.hF);
                    while (x < (y ))
                    {
                        matriz[i, (x-min)] = "1";
                        x++;
                    }



                }
                i++;
            }


//            <tr>
//<td >08:00-09:00</td>
//<td  class="tooltip libre" > <span class="classic ">07:20-07:40</span>         </td>

//<td> <input  type="submit" value="           " ></td>
//<td> <input  type="submit" value="           " ></td>
//<td> <input type="submit" value="           " class="cd3"></td>
//<td> <input type="submit" value="           " class="cd3"></td>
//<td> <input type="submit" value="           " class="cd2"></td>
//<td> <input type="submit" value="           " class="cd1"></td>
//</tr>

        string texto="";

            
            string[] fechas= getFechas(fecha);



            texto += "   <table id=\"Table1\"  border=\"1\"></td> <th>Hora:</th><th>" + fechas[0] + "<br>Lunes</th><th>" + fechas[1] + "<br>Martes</th><th>" + fechas[2] + "<br>Miercoles</th><th>" + fechas[3] + "<br>Jueves</th><th>" + fechas[4] + "<br>Viernes</th><th>" + fechas[5] + "<br>Sabado</th><th>" + fechas[6] + "<br>Domingo</th><tbody id=\"bodyMP\">";




            int auxMin=min;
            for (int m = 0; m < (max - min); m++)
            {
                for (int n = -1; n < 7; n++)
                {
                    if (n == -1)
                    {
                        texto += "<tr> <td >" + auxMin + ":00-" + (auxMin + 1) + ":00</td>";
                        auxMin++;
                    }
                    else
                    {
                        if (matriz[n, m] == "-1")
                        {
                            texto += "<td  class=\"tooltip noDisponible\" > <span class=\"classic \">No disponible.</span>         </td>";
                        }
                        if (matriz[n, m] == "0")
                        {
                            texto += "<td  class=\"tooltip libre tiempo\" > <span class=\"classic \">Libre.</span>         </td>";
                        }
                        if (matriz[n, m] == "1")
                        {
                            texto += "<td  class=\"tooltip ocupado\" > <span class=\"classic tiempo \">Ocupado.</span>         </td>";

                        }
                        
                        


                    }

                }
                texto += "</tr>";
            }

            return Json(new { texto });
        }

        
        [HttpPost]
        public JsonResult verDisponibilidadBuscar(FiltroSolicitudProdcuto idDepartamento)
        {
            List<CabeceraReserva> listaReserva = reservaDao.verDisponibilidadBuscar(idDepartamento);

            return Json(new { listaReserva });
        }

          [HttpPost]
        public JsonResult cargarAreaComun(int idDepartamento)
        {
            List<AreaComun_reserva> listaAreaComun = reservaDao.cargarAreaComun(idDepartamento);

            return Json(new { listaAreaComun });
        }

        public string[] getFechas(DateTime fecha){
            int i=0;
            switch (fecha.DayOfWeek){
                case DayOfWeek.Monday:
                    i=1;break;
                    case DayOfWeek.Tuesday:
                    i=2;break;
                    case DayOfWeek.Wednesday:
                    i=3;break;
                    case DayOfWeek.Thursday:
                    i=4;break;
                    case DayOfWeek.Friday:
                    i=5;break;
                    case DayOfWeek.Saturday:
                    i=6;break;
                    case DayOfWeek.Sunday:
                    i=7;break;
            }
            string[] fechas=new string[7];
            for(int j=0;j<7;j++){
                fechas[j]= fecha.AddDays(-i+1).ToShortDateString();
                i--;


            }


            return fechas;



        }


        
                [HttpPost]
        public JsonResult eliminarReserva(int idReserva)
        {
            string mensaje = reservaDao.eliminarReserva(idReserva);

            return Json(new { mensaje });
        }




    }
}
